﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using MySql.Data.MySqlClient;
using System.Text;
using cmsservice.Common.Memcached;

/// <summary>
///BBSDal 的摘要说明
/// </summary>
public class BBSDAL
{
    public DataTable GeteData(string id, int count)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GeteData." + id + "." + count;
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            MySqlDataAdapter adapter = null;
            string sql = "select tid,subject,author,dateline from  pre_forum_post where fid=" + id + " and invisible=0 and first=1  order by dateline desc limit 0 ," + count;
            adapter = new MySqlDataAdapter(sql, DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    public DataTable Get论坛精华()
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GeteLTJH.AACC";
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select  pre_forum_post.tid,pre_forum_post.subject,pre_forum_post.author,pre_forum_post.dateline from pre_forum_thread ");
            sb.AppendLine("inner join pre_forum_post on pre_forum_thread.tid=pre_forum_post.tid");
            sb.AppendLine("where digest>0 and  invisible=0 and first=1");
            sb.AppendLine("order by pre_forum_thread.dateline desc limit 0 ,50");
            MySqlDataAdapter adapter = null;
            adapter = new MySqlDataAdapter(sb.ToString(), DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    /// <summary>
    /// 多少小时内热贴
    /// </summary>
    /// <param name="h">为负整数</param>
    /// <returns></returns>
    public DataTable GetHots(int h)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GetHots." + h;
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            long seconds = (long)(DateTime.Now.AddHours(-h) - DateTime.Parse("1970-1-1")).TotalSeconds;
            sb.AppendFormat(" select tid,subject,author,dateline ");
            sb.AppendFormat(" from pre_forum_thread ");
            sb.AppendFormat(" where dateline>{0}", seconds);
            sb.AppendFormat(" order by heats desc limit 0 ,50");
            MySqlDataAdapter adapter = null;
            adapter = new MySqlDataAdapter(sb.ToString(), DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;

        }
    }

    public DataTable Get板块表(string[] tids)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GetBKB." + string.Join(".", tids);
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            string ids = string.Empty;
            for (int i = 0; i < tids.Length; i++)
            {
                ids += tids[i] + ",";
            }
            if (ids.Length > 0)
            {
                ids = ids.Substring(0, ids.Length - 1);
            }
            string str = "select * from pre_forum_forum where fid in (" + ids + ")";
            MySqlDataAdapter adapter = null;
            adapter = new MySqlDataAdapter(str, DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    /// <summary>
    /// 得到论坛内容
    /// </summary>
    /// <param name="tid">帖子编号</param>
    /// <returns></returns>
    public DataTable GetContents(long tid)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GetContents." + tid;
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            string sql = "select * from pre_forum_post where tid=" + tid + " and invisible=0 and first=1";
            MySqlDataAdapter adapter = null;
            adapter = new MySqlDataAdapter(sql, DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    /// <summary>
    /// 得到论坛内容
    /// </summary>
    /// <param name="tid">栏目编号</param>
    /// <returns></returns>
    public DataTable GetContentsByFid(int fid, int count)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GetContentsByFid." + fid + "." + count;
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            string sql = "select * from pre_forum_post where fid=" + fid + " and invisible=0 and first=1 order by dateline desc limit 1," + count;
            MySqlDataAdapter adapter = null;
            adapter = new MySqlDataAdapter(sql, DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    public DataTable GetAttachment(long aid)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GetAttachment." + aid;
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            string sql = "select * from pre_forum_attachment where aid=" + aid;
            MySqlDataAdapter adapter = null;
            adapter = new MySqlDataAdapter(sql, DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    public DataTable GeteDataByTypeid(int typeid, int count)
    {
        return new DataTable();
        News2CacheTools cache = new News2CacheTools();
        string key = "BBSDAL.GeteDataByTypeid." + typeid + "." + count;
        var ds = cache.Get<DataSet>(key);
        if (ds != null && ds.Tables.Count > 0)
        {
            return ds.Tables[0];
        }
        else
        {
            MySqlDataAdapter adapter = null;
            string sql = "select a.*,b.typeid from  pre_forum_post as a left join pre_forum_thread as b on a.tid=b.tid where b.typeid=" + typeid + " and a.invisible=0 and a.first=1 order by a.dateline desc limit 0 ," + count;
            adapter = new MySqlDataAdapter(sql, DBManager.BBS);
            DataTable tb = new DataTable();
            adapter.Fill(tb);
            MySqlDispose(adapter);
            ds = new DataSet();
            ds.Tables.Add(tb);
            cache.Add(key, ds, 0, 30, 0);
            return tb;
        }
    }

    public void MySqlDispose(MySqlDataAdapter c)
    {
        try
        {
            if (c != null)
            {
                if (c.SelectCommand != null)
                {
                    if (c.SelectCommand.Connection != null)
                    {
                        c.SelectCommand.Connection.Close();
                    }
                }
            }
        }
        catch
        {
        }

    }
}